//
//  LZMyRecordsViewController.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyRecordsViewController.h"
#import "HTHorizontalSelectionList.h"
#import "LZMyVideoAndRestaurantsBaseScrollView.h"
#import "LZMyRecordVideoTableView.h"
#import "LZMyRecordRestaurantsTableView.h"
#import "LZMyReviewedTableView.h"
#import "LAMerchantDetailViewController.h"
#import "LZVideoCellPresenter.h"
#import "LZUserCenterCollectAndCommentPresenter.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZAlertView.h"
#import "LZRestaurantListModel.h"
#import "LZUserOperationPresenter.h"
#import "LZZanAndCollectStatusPresenter.h"
#import "Masonry.h"

@interface LZMyRecordsViewController ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource,LZMyRestaurantsBaseTableViewDelegate,LZVideoCellDelegate,LZMyVideoAndRestaurantsBaseTableViewDelegate,LZRestaurantTableViewCellButtonClickDelegate>

@property (nonatomic, strong) HTHorizontalSelectionList * headerListView;
@property (nonatomic, strong) LZMyVideoAndRestaurantsBaseScrollView *scroll;
@property (nonatomic, strong) NSArray *carMakes;

@property (strong, nonatomic) UIButton *rightButton;

@property (strong, nonatomic) LZVideoCellPresenter *videoCellPresenter;

@end

@implementation LZMyRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configDataByType];
    [self setup];
    [self addAutoLayout];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    [self.scroll viewShow];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.scroll viewDiss];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - request method

// ************************************** 我的评论列表 **************************************

- (void)requestReviewListWithReviewType:(ReviewType)type andPrameter:(NSDictionary*)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSString *))successBlock {
    
    if (type == ReviewType_Restaurants) {
        //  我的餐厅评论列表
       [LZUserCenterCollectAndCommentPresenter getUserRestaurantCommentListWithPageNum:page PageLimit:limit ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
           if (successBlock) {
               successBlock(listArr,timestamp);
           }
       } ResultFailed:^(NSString *remind) {
           if (successBlock) {
               successBlock(nil,nil);
           }
       }];
    }
    else if (type == ReviewType_Video) {
        //  我的视频评论列表
        [LZUserCenterCollectAndCommentPresenter getUserVideoCommentListWithPageNum:page PageLimit:limit ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
            if (successBlock) {
                successBlock(listArr,timestamp);
            }
        } ResultFailed:^(NSString *remind) {
            if (successBlock) {
                successBlock(nil,nil);
            }
        }];
    }
}

// ************************************** 我的收藏列表 **************************************
- (void)requestFavoriteListWithReviewTag:(NSInteger)tag andPrameter:(NSDictionary*)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSString *))successBlock {
    if (tag == 1) {
        // 我的收藏——视频列表
        [LZUserCenterCollectAndCommentPresenter getUserCollectListWithType:@"1" PageNum:page PageLimit:limit andPrameter:prameter ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
            if (successBlock) {
                successBlock(listArr,timestamp);
            }
        } ResultFailed:^(NSString *remind) {
            if (successBlock) {
                successBlock(nil,nil);
            }
        }];
    }
    else if (tag == 2) {
        // 我的收藏——餐厅列表
        [LZUserCenterCollectAndCommentPresenter getUserCollectListWithType:@"0" PageNum:page PageLimit:limit andPrameter:prameter ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
            if (successBlock) {
                successBlock(listArr,timestamp);
            }
        } ResultFailed:^(NSString *remind) {
            if (successBlock) {
                successBlock(nil,nil);
            }
        }];
    }
}

// ************************************** 餐厅--------收藏/取消收藏 **************************************
- (void)requestCollect:(BOOL)isCollect andRestaurantId:(NSString*)restaurantId successBlock:(void (^)(BOOL))successBlock{
    
    [LZUserOperationPresenter collectOrCancelCollectVideoOrRestaurantWithSellerID:restaurantId PromotionID:nil FavoriteType:@"0" OperationType:[NSString stringWithFormat:@"%d",isCollect] ResultSuccess:^(NSInteger totleCount, NSInteger operationType) {
        DLog(@"-==-=====收藏成功");
        [[LZZanAndCollectStatusPresenter shareInstance] addCollectRsetaurantWithId:restaurantId andCollectStatus:isCollect];
        if (successBlock) {
            successBlock(YES);
        }

    } ResultFailed:^(NSString *remind) {
        DLog(@"%@",remind);
    }];
    
}

// ************************************** 待点评列表 **************************************
- (void)requestNotRemarkListWithReviewTag:(NSInteger)tag andPrameter:(NSDictionary*)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSString *))successBlock {
    if (tag == 1) {
        // 我的待点评——视频列表
        [LZUserCenterCollectAndCommentPresenter getUserWaitCommentListWithType:@"1" PageNum:page PageLimit:limit andPrameter:(NSDictionary*)prameter ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
            if (successBlock) {
                successBlock(listArr,timestamp);
            }
        } ResultFailed:^(NSString *remind) {
            if (successBlock) {
                successBlock(nil,nil);
            }
        }];
    }
    else if (tag == 2) {
        // 我的待点评—餐厅列表
        [LZUserCenterCollectAndCommentPresenter getUserWaitCommentListWithType:@"0" PageNum:page PageLimit:limit andPrameter:(NSDictionary*)prameter ResultSuccess:^(NSArray *listArr,NSString *timestamp) {
            if (successBlock) {
                successBlock(listArr,timestamp);
            }
        } ResultFailed:^(NSString *remind) {
            if (successBlock) {
                successBlock(nil,nil);
            }
        }];
    }
}

#pragma
#pragma mark - private method

#pragma mark -
#pragma mark configure UI

- (void)configDataByType {
    switch (self.vcType) {
        case MyRecordsType_ToBeReviewed:
        {
            self.umLogPageViewName = @"待评价";
            self.title = LZLocalizedString(@"mine_to_be_reviewed", nil);
        }
            break;
            
        case MyRecordsType_Reviewed:
        {
            self.umLogPageViewName = @"评价页面";
            self.title = LZLocalizedString(@"mine_my_reviews", @"");//@"Reviewed";
            self.carMakes = @[LZLocalizedString(@"mine_video", nil), LZLocalizedString(@"mine_restaurants", nil)];

        }
            break;
            
        case MyRecordsType_History:
        {
            self.umLogPageViewName = @"流量历史页面";
            self.title = @"Browse history";
            self.carMakes = @[LZLocalizedString(@"mine_video", nil), LZLocalizedString(@"mine_restaurants", nil)];
            [self configNavigationBarButton];
        }
            break;
            
        case MyRecordsType_Favorites:
        {
            self.umLogPageViewName = @"我的收藏页面";
            self.title = LZLocalizedString(@"mine_my_favorites", @"");
            self.carMakes = @[LZLocalizedString(@"mine_favorite_videos", nil), LZLocalizedString(@"mine_favorite_restaurants", nil)];
            
        }
            break;
    }
}


- (void)configNavigationBarButton {
    // 提交按钮
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightButton setTitle:LZLocalizedString(@"mine_clear_history", nil) forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.rightButton setTitleColor:UIColorFromRGB(0x666666, 1) forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    self.rightButton.frame = CGRectMake(0, 0, CGRectGetWidth(self.rightButton.bounds), 40);
    [self.rightButton addTarget:self action:@selector(navigationBarRightButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self navigationItemSetting:@[self.rightButton] type:NAVIGATIONITEMSETTING_RIGHT];
}

- (void)navigationBarRightButtonClickAction:(UIButton*)sender {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    [self showClearAlert];
}

- (void)setCarMakes:(NSArray *)carMakes {
    _carMakes = carMakes;
}

- (void)setup {
    
    if (self.vcType != MyRecordsType_ToBeReviewed) {
        [self.view addSubview:self.headerListView];
    }
    [self.view addSubview:self.scroll];
    [self configTableViewsDelegate];
}

- (void)addAutoLayout {
    
    if (self.vcType != MyRecordsType_ToBeReviewed) {
        [self.headerListView makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.leading.mas_equalTo(0);
            make.trailing.mas_equalTo(0);
            make.height.mas_equalTo(52);
        }];
    }
    
    [self.scroll makeConstraints:^(MASConstraintMaker *make) {
        if (self.vcType == MyRecordsType_ToBeReviewed) {
            make.top.mas_equalTo(0);
        }
        else {
            make.top.mas_equalTo(self.headerListView.mas_bottom);
        }
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)configTableViewsDelegate {
    
    if (self.vcType == MyRecordsType_Reviewed) {
        self.scroll.reviewedVideoTable.subTableViewDelegate = self;
        self.scroll.reviewedRestaurantsTable.subTableViewDelegate = self;
        
        [self.scroll.reviewedVideoTable checkHaveData];
    }
    else {
        self.scroll.videoTable.videoDelegate = self;
        self.scroll.videoTable.subTableViewDelegate = self;
        
        if (self.vcType != MyRecordsType_ToBeReviewed) {
            self.scroll.restaurantsTable.restaurantTableDelegate = self;
            self.scroll.restaurantsTable.subTableViewDelegate = self;
            self.scroll.restaurantsTable.clickButtonDelegate = self;
        }
        
        [self.scroll.videoTable checkHaveData];
    }
}

#pragma mark - LZMyRestaurantsBaseTableViewDelegate

- (void)restaurantsBaseTableViewDidSelectRowAtIndexPath:(NSIndexPath *)indexPath object:(id)object {
    LZRestaurantListModel *model = (LZRestaurantListModel*)object;
    LAMerchantDetailViewController *vc = [[LAMerchantDetailViewController alloc] init];
    vc.restaurantId = [model.restaurantId copy];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - LZRestaurantTableViewCellButtonClickDelegate
// 点击按钮
- (void)clickButtonWithModel:(id)model andIndexPath:(NSIndexPath *)indexPath andResultBlock:(void (^)(BOOL))resultBlock {
    DLog(@"点击按钮");
    LZRestaurantListModel *restaurantModel = model;
    if (self.vcType == MyRecordsType_Favorites) {
        [self requestCollect:!restaurantModel.isStore andRestaurantId:restaurantModel.restaurantId successBlock:resultBlock];
        
    }
    else if (self.vcType == MyRecordsType_ToBeReviewed) {
        // 第一版需求已去掉
    }
}

#pragma mark - LZMyVideoAndRestaurantsBaseTableViewDelegate
- (void)requestDateWithTag:(NSInteger)tag andPrameter:(NSDictionary *)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSString *))successBlock {
    
    switch (self.vcType) {
        case MyRecordsType_ToBeReviewed:
        {
            // 我的待评价列表
             [self requestNotRemarkListWithReviewTag:tag andPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
        }
            break;
        case MyRecordsType_Reviewed:
        {
            // 我的评价列表
            [self requestReviewListWithReviewType:tag andPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
        }
            break;
        case MyRecordsType_Favorites:
        {
            // 我的收藏
            [self requestFavoriteListWithReviewTag:tag andPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
        }
            break;
        case MyRecordsType_History:
        {
            // 我的浏览历史
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - LZMyVideoBaseTableViewDelegate
#pragma mark -
#pragma mark cell  delegate
/**
 cell 按钮协议方法
 index: 0点赞 1收藏  2信息  3分享   4更多  5抢券详情   6拼团详情
 */

- (void)clickCellButtonWithIndex:(VideoCellDelegateClickEventCode)index andCellIndexPath:(NSIndexPath *)cellIndexPath andModel:(id)model andOtherObject:(id)object refreshBlock:(void (^)(VideoCellRefreshType, BOOL))refreshBlock {
    
    [self.videoCellPresenter dealClickCellButtonIndex:index andCellIndexPath:cellIndexPath andNavigationController:self.navigationController andHidesBottomBarWhenPushed:NO andCellModel:model andOtherObject:object andRefreshBlcok:refreshBlock];
}

- (LZVideoCellPresenter*)videoCellPresenter {
    if (_videoCellPresenter) {
        return _videoCellPresenter;
    }
    
    _videoCellPresenter = [[LZVideoCellPresenter alloc] init];
    return _videoCellPresenter;
}

#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.carMakes.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.carMakes[index];
}

#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    
    DLog(@"select : %ld", (long)index);
    [self.scroll clickButtonTag:index];
}

#pragma mark - Setter/Getter

- (LZMyVideoAndRestaurantsBaseScrollView *)scroll {
    
    if (_scroll) {
        return _scroll;
    }
    
    _scroll = [[LZMyVideoAndRestaurantsBaseScrollView alloc] initWithFrame:CGRectZero andType:self.vcType];
    
    return _scroll;
}

- (HTHorizontalSelectionList *)headerListView {
    
    if (_headerListView == nil) {
        _headerListView = [[HTHorizontalSelectionList alloc] init];
        [_headerListView setScrollViewColor:UIColorFromRGB(0xffffff, 1)];
        _headerListView.delegate = self;
        _headerListView.dataSource = self;
    }
    
    return _headerListView;
}

#pragma mark - 弹Alert
/**
 *  @author LY, 16-08-23 19:08:06
 *
 *  弹出提示是否清除的alert
 */
-(void)showClearAlert {
    
    LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"mine_clear_remind", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
                       
    [alert show];
    
    alert.clickButtonIndexBlock = ^(NSInteger index) {
//        NSLog(@"恩，确实点击了第%ld个按钮",(long)index);
        
    };
}


@end
